﻿<html>

<head>
<link rel="shortcut icon" type="image/png" href="../favicon.png">
<meta http-equiv=Content-Type content="text/html; charset=UTF-8">
<meta name=Generator content="Microsoft Word 15 (filtered)">
<title>ClearScript FAQtorial</title>
<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Cambria;
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Consolas;
	panose-1:2 11 6 9 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin-top:6.0pt;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:0in;
	margin-bottom:.0001pt;
	line-height:115%;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
h1
	{mso-style-link:"Heading 1 Char";
	margin-top:10.0pt;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:0in;
	margin-bottom:.0001pt;
	line-height:115%;
	page-break-after:avoid;
	font-size:14.0pt;
	font-family:"Cambria",serif;
	color:#365F91;}
p.MsoHeader, li.MsoHeader, div.MsoHeader
	{mso-style-link:"Header Char";
	margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
p.MsoFooter, li.MsoFooter, div.MsoFooter
	{mso-style-link:"Footer Char";
	margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
p.MsoTitle, li.MsoTitle, div.MsoTitle
	{mso-style-link:"Title Char";
	margin-top:6.0pt;
	margin-right:0in;
	margin-bottom:15.0pt;
	margin-left:0in;
	border:none;
	padding:0in;
	font-size:26.0pt;
	font-family:"Cambria",serif;
	color:#17365D;
	letter-spacing:.25pt;}
p.MsoTitleCxSpFirst, li.MsoTitleCxSpFirst, div.MsoTitleCxSpFirst
	{mso-style-link:"Title Char";
	margin-top:6.0pt;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:0in;
	margin-bottom:.0001pt;
	border:none;
	padding:0in;
	font-size:26.0pt;
	font-family:"Cambria",serif;
	color:#17365D;
	letter-spacing:.25pt;}
p.MsoTitleCxSpMiddle, li.MsoTitleCxSpMiddle, div.MsoTitleCxSpMiddle
	{mso-style-link:"Title Char";
	margin:0in;
	margin-bottom:.0001pt;
	border:none;
	padding:0in;
	font-size:26.0pt;
	font-family:"Cambria",serif;
	color:#17365D;
	letter-spacing:.25pt;}
p.MsoTitleCxSpLast, li.MsoTitleCxSpLast, div.MsoTitleCxSpLast
	{mso-style-link:"Title Char";
	margin-top:0in;
	margin-right:0in;
	margin-bottom:15.0pt;
	margin-left:0in;
	border:none;
	padding:0in;
	font-size:26.0pt;
	font-family:"Cambria",serif;
	color:#17365D;
	letter-spacing:.25pt;}
a:link, span.MsoHyperlink
	{color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
	{margin-top:6.0pt;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:.5in;
	margin-bottom:.0001pt;
	line-height:115%;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpFirst, li.MsoListParagraphCxSpFirst, div.MsoListParagraphCxSpFirst
	{margin-top:6.0pt;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:.5in;
	margin-bottom:.0001pt;
	line-height:115%;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpMiddle, li.MsoListParagraphCxSpMiddle, div.MsoListParagraphCxSpMiddle
	{margin-top:0in;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:.5in;
	margin-bottom:.0001pt;
	line-height:115%;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpLast, li.MsoListParagraphCxSpLast, div.MsoListParagraphCxSpLast
	{margin-top:0in;
	margin-right:0in;
	margin-bottom:0in;
	margin-left:.5in;
	margin-bottom:.0001pt;
	line-height:115%;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
span.TitleChar
	{mso-style-name:"Title Char";
	mso-style-link:Title;
	font-family:"Cambria",serif;
	color:#17365D;
	letter-spacing:.25pt;}
span.Heading1Char
	{mso-style-name:"Heading 1 Char";
	mso-style-link:"Heading 1";
	font-family:"Cambria",serif;
	color:#365F91;
	font-weight:bold;}
span.HeaderChar
	{mso-style-name:"Header Char";
	mso-style-link:Header;}
span.FooterChar
	{mso-style-name:"Footer Char";
	mso-style-link:Footer;}
.MsoChpDefault
	{font-family:"Calibri",sans-serif;}
.MsoPapDefault
	{margin-bottom:10.0pt;
	line-height:115%;}
 /* Page Definitions */
 @page WordSection1
	{size:8.5in 11.0in;
	margin:.5in .5in .5in .5in;}
div.WordSection1
	{page:WordSection1;}
 /* List Definitions */
 ol
	{margin-bottom:0in;}
ul
	{margin-bottom:0in;}
-->
</style>

</head>

<body lang=EN-US link=blue vlink=purple style="background-color: gray">

<div class=WordSection1 style="width: 9in; margin: auto; zoom: 1.2; background-color: white; padding: .25in">

<div style='border:none;border-bottom:solid #4F81BD 1.0pt;padding:0in 0in 4.0pt 0in'>

<p class=MsoTitle style='margin-top:0in'>ClearScript FAQtorial</p>

</div>

<h1 style='margin-left:.25in;text-indent:-.25in'>1. What’s a FAQtorial?</h1>

<p class=MsoNormal style='margin-left:.25in'>It’s a tutorial in the form of questions
and answers. It may look like a FAQ list, but it’s really a guide.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>2. OK, so what’s ClearScript?</h1>

<p class=MsoNormal style='margin-left:.25in'><a name="_Hlk482337160">ClearScript
is a library that allows you to add scripting to your .NET applications. It
supports </a><a
href="https://msdn.microsoft.com/en-us/library/hbxc2t98(v=vs.84).aspx">JScript</a>
and <a href="https://msdn.microsoft.com/en-us/library/t0aew7h6(v=vs.84).aspx">VBScript</a>
out of the box and in theory can work with other <a
href="http://msdn.microsoft.com/en-us/library/bb871518.aspx">Windows Script</a>
engines.</p>

<p class=MsoNormal style='margin-left:.25in'><b><i><span style='color:#00B050'>New!</span></i></b>
<a name="_Hlk482337197">ClearScript 5 supports the </a><a name="_Hlk482337218"></a><a
href="https://developers.google.com/v8/">V8</a> high-performance open-source JavaScript
engine. Unlike Windows Script engine instances, V8 instances have no thread
affinity and are suitable for server-side asynchronous scripting.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>3. Hasn’t that been done
before?</h1>

<p class=MsoNormal style='margin-left:.25in'>Perhaps, but sometimes it’s fun to
start something from scratch and see how far you can take it.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>4. Why would I want to add
scripting to my application anyway?</h1>

<p class=MsoNormal style='margin-left:.25in'>There are many possible reasons. Sophisticated
productivity applications often support script-based customization and
extension. The ability to automate an application may be desirable for testing purposes
or for running complex jobs. Another goal might be to process active content
that includes embedded scripts, such as web pages.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>5. Doesn’t .NET have its own
scripting technologies?</h1>

<p class=MsoNormal style='margin-left:.25in'>Yes, but they’re designed for a
different purpose. They typically introduce numerous language extensions in
order to “retarget” standard script languages for full-blown .NET development.
That is <i>not</i> ClearScript’s goal. ClearScript focuses on adding
standards-based scripting to <i>existing</i> .NET applications with minimal
effort.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>6. OK, but if it’s been done
before, what’s so special about ClearScript?</h1>

<p class=MsoNormal style='margin-left:.25in'>ClearScript is all about
simplicity. It’s one line of code to instantiate a script engine, another to
expose a host resource for scripting, and one more to execute a script. You
don’t worry about implementing hosting interfaces, creating scriptable proxies,
decorating class members, etc.</p>

<p class=MsoNormal style='margin-left:.25in'>Because it aims to make existing applications
scriptable without modification or special coding, ClearScript works hard to give
script code the ability to access a long list of .NET features – constructors,
methods, properties, fields, events, indexers, extension methods, nested types,
output parameters, statics, generics, enums, delegates, etc. It does this
without introducing special syntax or requiring script language extensions of
any kind.</p>

<p class=MsoNormal style='margin-left:.25in'>ClearScript also provides
streamlined host-to-script support, allowing the host to invoke script
functions and access script objects directly. In addition, it allows script-enabled
applications to be debugged in script mode.</p>

<p class=MsoNormal style='margin-left:.25in'><b><i><span style='color:#00B050'>New!</span></i></b>
ClearScript 5 supports simultaneous debugging of script, managed, and native code
for applications that use its new V8-based JavaScript engine.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>7. Fine, so how do I
instantiate a script engine with ClearScript?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>You simply “new up” a class instance:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host - C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> <b>Microsoft.ClearScript.V8</b>;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:blue'>var</span><span style='font-size:10.0pt;font-family:Consolas'>
engine = <span style='color:blue'>new</span> <b><span style='color:#2B91AF'>V8ScriptEngine</span></b>();</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>At this point the script engine contains
only its built-in objects and functions and has no access to the host
application.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>8. That’s not very useful; how
do I expose my host API for scripting?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>You make one method call to expose a host object or type:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host - C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.<b>AddHostObject</b>(<span style='color:#A31515'>&quot;uri&quot;</span>,
<span style='color:blue'>new</span> <span style='color:#2B91AF'>Uri</span>(<span
style='color:#A31515'>&quot;http://www.example.com&quot;</span>));</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.<b>AddHostType</b>(<span
style='color:#A31515'>&quot;Console&quot;</span>, <span style='color:blue'>typeof</span>(<span
style='color:#2B91AF'>Console</span>));</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>The script engine now contains two
root-level objects – a <i>host object</i> and a <i>host type</i> – that provide
access to the corresponding host resources.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>9. What can script code do
with a host object?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>It can access the object’s public members directly:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var uriQuery = <b>uri.Query</b>;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var uriString = <b>uri.ToString</b>();</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>10. What can script code do
with a host type?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>It can access the type’s static members and nested types
directly:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><b><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>Console.WriteLine</span></b><span
style='font-size:10.0pt;font-family:Consolas;color:#A31515'>('Downloading from
{0}...', uri);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><b><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>Console.Title</span></b><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'> = 'Please wait...';</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>Host types can also be used to
invoke constructors and specify generic type arguments. There’ll be more on
that later.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>11. Once my host API is
exposed, how do I actually run a script?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Use one of these methods:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host - C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.<b>Execute</b>(<span style='color:#A31515'>&quot;var
uriQuery = uri.Query;&quot;);</span></span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:blue'>var</span><span style='font-size:10.0pt;font-family:Consolas'>
result = engine.<b>Evaluate</b>(<span style='color:#A31515'>&quot;Math.sqrt(Math.PI)&quot;</span>);</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>12. Why have two different
methods for running scripts?</h1>

<p class=MsoNormal style='margin-left:.25in'>Some script languages have ambiguous
syntactic elements whose meaning depends on whether they’re part of a statement
or an expression. For example, in VBScript a single equals sign is used for
both assignment (statement) and equality testing (expression). To resolve this
ambiguity the host must establish the syntactic context for script execution. <b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Execute</span></b>
interprets script code in a statement context, while <b><span style='font-size:
10.0pt;line-height:115%;font-family:Consolas'>Evaluate</span></b> treats code
as an expression.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>13. You’ve covered host
methods and properties. What about fields?</h1>

<p class=MsoNormal style='margin-left:.25in'>To script code, the public fields
of a host object or type look exactly like properties.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>14. How do I call generic
methods?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>ClearScript supports C#-like type inference, so you can call
most generic methods just as you’d call them from C#. However, some generic
methods require explicit type arguments. To call such a method from script
code, place the required number of host types at the beginning of the argument
list. For example, suppose the host exposes the following types:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System.Linq;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.AddHostType(<span style='color:#A31515'>&quot;Int32&quot;</span>,
<span style='color:blue'>typeof</span>(<span style='color:blue'>int</span>));</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostType(<span
style='color:#A31515'>&quot;Enumerable&quot;</span>, <span style='color:blue'>typeof</span>(<span
style='color:#2B91AF'>Enumerable</span>));</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>Here’s how to call the <a
href="http://msdn.microsoft.com/en-us/library/bb341042.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Enumerable.Empty</span></b></a>
generic method from script code:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var empty = Enumerable.<b>Empty(Int32)</b>;</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>15. What about extension
methods?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Extension methods are available if the type that implements
them has been exposed. Continuing the previous example, here’s script code that
calls the <a href="http://msdn.microsoft.com/en-us/library/bb298736.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Enumerable.ToArray</span></b></a>
extension method:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var numbers = Enumerable.Range(0, 10).<b>ToArray()</b>;</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>Note that <a
href="http://msdn.microsoft.com/en-us/library/bb298736.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Enumerable.ToArray</span></b></a>
is a generic extension method whose type argument is inferred from its “this”
argument. As you can see, ClearScript allows you to call it using the same
convenient syntax as C#.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>16. Can I handle an event
using a script function?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes. Here’s how to connect a host event source to a script
handler function:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var <b>connection</b> =
Console.CancelKeyPress.<b>connect</b>(function (sender, args) {</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>    Console.WriteLine('{0} pressed!',
args.SpecialKey);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>});</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>To break the connection, do this:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>connection.<b>disconnect</b>();</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>17. How do I create an instance
of a host type from script code?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Script code cannot instantiate a host type directly.
However, ClearScript provides a set of utility functions that includes this
capability:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> Microsoft.ClearScript;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.AddHostObject(<span style='color:#A31515'>&quot;host&quot;</span>,
<span style='color:blue'>new</span> <b><span style='color:#2B91AF'>HostFunctions</span></b>());</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostType(<span
style='color:#A31515'>&quot;Uri&quot;</span>, <span style='color:blue'>typeof</span>(<span
style='color:#2B91AF'>Uri</span>));</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>Now you can create <a
href="http://msdn.microsoft.com/en-us/library/system.uri.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>System.Uri</span></b></a>
class instances from script code:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var newUri = <b>host.newObj</b>(<b>Uri</b>, 'http://www.example.com');</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>Note that <a
href="https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_HostFunctions_newObj__1.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>newObj</span></b></a>
is a generic method that requires an explicit type argument. Its first argument
(the type argument) is the host type to be instantiated. The rest of the
arguments are passed to the constructor that matches the argument signature.</p>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'><b><i><span style='color:#00B050'>New!</span></i></b> ClearScript
5 supports simplified instantiation syntax for JavaScript:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var newUri = <b>new Uri</b>('http://www.example.com');</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>18. What about an array?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>There’s a utility function for creating host arrays, too:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var uriArray = host.<b>newArr</b>(Uri, 10);</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>19. Can I access host array
elements by index?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>That depends on your script language. However, if a host
object implements <a
href="http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>System.Collections.IList</span></b></a>,
ClearScript exposes read/write properties with numeric names that match the
object’s valid indices:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var firstUri = <b>uriArray[</b>0<b>]</b>;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><b><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>uriArray[</span></b><span style='font-size:10.0pt;font-family:
Consolas;color:#A31515'>1<b>]</b> = firstUri;</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>20.  Can I call methods with
output parameters from script code?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes. Suppose that a dictionary is exposed for scripting:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System.Collections.Generic;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostObject(<span
style='color:#A31515'>&quot;dict&quot;</span>, <span style='color:blue'>new</span>
<span style='color:#2B91AF'>Dictionary</span>&lt;<span style='color:blue'>string</span>,
<span style='color:#2B91AF'>Uri</span>&gt;());</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>You can use a <i>host variable</i> to invoke the <a
href="http://msdn.microsoft.com/en-us/library/bb347013.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Dictionary.TryGetValue</span></b></a>
method from script code:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var <b>uriVar</b> = host.<b>newVar</b>(Uri);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>if (dict.TryGetValue('key', <b>uriVar.out</b>))
{</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>    Console.WriteLine('Found Uri value:
{0}.', <b>uriVar</b>);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>}</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>Host variables support two special
properties in addition to <b><span style='font-size:10.0pt;line-height:115%;
font-family:Consolas'>out</span></b>. The first is <b><span style='font-size:
10.0pt;line-height:115%;font-family:Consolas'>ref</span></b>, which is similar
to <b><span style='font-size:10.0pt;line-height:115%;font-family:Consolas'>out</span></b>
but compatible with <a
href="http://msdn.microsoft.com/en-us/library/14akc2c7.aspx"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>ref</span></b></a>
parameters. The second is <b><span style='font-size:10.0pt;line-height:115%;
font-family:Consolas'>value</span></b>, which provides read/write access to the
value stored within the variable. If you’re passing a variable into an input
parameter, invoking <b><span style='font-size:10.0pt;line-height:115%;
font-family:Consolas'>value</span></b> is unnecessary.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>21. Can I route a callback to a
script function?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes, by creating a delegate that invokes your script
function. Consider this code:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> System.Threading;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>engine.AddHostType(<span style='color:#A31515'>&quot;Timer&quot;</span>,
<span style='color:blue'>typeof</span>(<span style='color:#2B91AF'>Timer</span>));</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostType(<span
style='color:#A31515'>&quot;TimerCallback&quot;</span>, <span style='color:
blue'>typeof</span>(<span style='color:#2B91AF'>TimerCallback</span>));</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>You can now create timers and handle their callbacks
from script code:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var <b>callback</b> = host.<b>del</b>(TimerCallback,
function (state) {</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>    Console.WriteLine('Timer fired: {0}.',
state);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>});</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var timer = host.newObj(Timer, <b>callback</b>, 'foo', 5000,
5000);</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
0in;margin-left:.25in;margin-bottom:.0001pt'>In addition to <a
href="https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_HostFunctions_del__1.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>del</span></b></a>,
ClearScript provides <a
href="https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_HostFunctions_proc.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>proc</span></b></a>
and <a
href="https://microsoft.github.io/ClearScript/Reference/html/Overload_Microsoft_ClearScript_HostFunctions_func.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>func</span></b></a>;
these functions create specialized delegates compatible with <b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>System.Action</span></b>
and <b><span style='font-size:10.0pt;line-height:115%;font-family:Consolas'>System.Func</span></b>
respectively.</p>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'><b><i><span style='color:#00B050'>New!</span></i></b>
ClearScript 5 supports simplified delegate creation syntax for JavaScript:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var callback =  <b>new TimerCallback</b>(function
(state) {</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>    Console.WriteLine('Timer fired: {0}.',
state);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>});</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>22. Can I expose many host
types in one step?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes. You can use a <i>host type collection</i> to expose all
the types defined in one or more assemblies:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host - C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> Microsoft.ClearScript;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>var</span><span style='font-size:10.0pt;
font-family:Consolas'> typeCollection = <span style='color:blue'>new</span> <b><span
style='color:#2B91AF'>HostTypeCollection</span></b>(<span style='color:#A31515'>&quot;mscorlib&quot;,
&quot;System&quot;, &quot;System.Core&quot;</span>);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostObject(<span
style='color:#A31515'>&quot;clr&quot;</span>, typeCollection);</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>Now all the types defined in the standard <b>mscorlib</b>,
<b>System</b>, and <b>System.Core</b> assemblies are exposed. Host type
collections are hierarchical data structures where leaf nodes represent host
types and parent nodes represent namespaces:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var guid = <b>clr.System.Guid</b>.NewGuid();</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var today = <b>clr.System.DayOfWeek</b>.Friday;</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>Note that, unlike C# types and namespaces, the nodes
of a host type collection are objects. A script can copy them to the root level
for more convenient access:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var System = clr.System;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var Guid = System.Guid;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var myGuid = Guid.NewGuid();</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>23. What if a host type refers
to an open generic type?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Such a host type can be invoked with type arguments to
produce a closed generic type:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var Dictionary =
System.Collections.Generic.Dictionary;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var myDictionary = host.newObj(<b>Dictionary(System.String,
System.Int32)</b>);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>myDictionary.Add('foo', 123);</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>24. Can I import host types
from script code?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Not without the host’s explicit permission:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:blue'>using</span><span style='font-size:10.0pt;
font-family:Consolas'> Microsoft.ClearScript;</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas'>…</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas'>engine.AddHostObject(<span
style='color:#A31515'>&quot;xHost&quot;</span>, <span style='color:blue'>new</span>
<b><span style='color:#2B91AF'>ExtendedHostFunctions</span></b>());</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>By exposing an instance of <a
href="https://microsoft.github.io/ClearScript/Reference/html/T_Microsoft_ClearScript_ExtendedHostFunctions.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>ExtendedHostFunctions</span></b></a>,
the host hands over the keys to the kingdom:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var List = <b>xHost.type</b>('System.Collections.Generic.List');</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var DayOfWeek = <b>xHost.type</b>('System.DayOfWeek');</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var week = xHost.newObj(List(DayOfWeek),
7);</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>week.Add(DayOfWeek.Sunday);</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>You can even import entire assemblies:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>var clr = xHost.<b>lib</b>('mscorlib', 'System',
'System.Core');</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>var Dictionary = clr.System.Collections.Generic.Dictionary;</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>25. Can the host call script
functions and access script objects directly?</h1>

<p class=MsoNormal style='margin-top:6.0pt;margin-right:0in;margin-bottom:10.0pt;
margin-left:.25in'>Yes. ClearScript provides a <a
href="http://msdn.microsoft.com/en-us/library/dd264736.aspx">dynamic</a>
property named <a
href="https://microsoft.github.io/ClearScript/Reference/html/P_Microsoft_ClearScript_ScriptEngine_Script.htm"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>Script</span></b></a>
that represents a script engine’s root-level namespace. Consider the following
script:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Script - JavaScript</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>function myFunc(x, y, z) {</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;page-break-after:
avoid;background:#F2F2F2;text-autospace:none'><span style='font-size:10.0pt;
font-family:Consolas;color:#A31515'>    return { xValue: x, yValue: y, zValue:
z };</span></p>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:#A31515'>}</span></p>

<p class=MsoNormal style='margin-top:14.0pt;margin-right:0in;margin-bottom:
10.0pt;margin-left:.25in'>After executing this script, the host can directly invoke
<b><span style='font-size:10.0pt;line-height:115%;font-family:Consolas'>myFunc</span></b>
and examine the properties of the object it returns:</p>

<div style='border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in;
background:#C6D9F1;margin-left:.5in;margin-right:.5in'>

<p class=MsoNormal style='line-height:normal;page-break-after:avoid;background:
#C6D9F1;text-autospace:none;border:none;padding:0in'><span style='font-size:
10.0pt;font-family:Consolas'>Host – C#</span></p>

</div>

<p class=MsoNormal style='margin-top:0in;margin-right:.5in;margin-bottom:0in;
margin-left:.5in;margin-bottom:.0001pt;line-height:normal;background:#F2F2F2;
text-autospace:none'><span style='font-size:10.0pt;font-family:Consolas;
color:blue'>var</span><span style='font-size:10.0pt;font-family:Consolas'> yValue
= engine.<b>Script.myFunc(</b>1, 2.0, <span style='color:#A31515'>&quot;three&quot;</span><b>).yValue</b>;</span></p>

<h1 style='margin-top:14.0pt;margin-right:0in;margin-bottom:0in;margin-left:
.25in;margin-bottom:.0001pt;text-indent:-.25in'>26. How do I debug script code?</h1>

<p class=MsoNormal style='margin-left:.25in'>If you’re using ClearScript with JScript,
VBScript, or another Windows Script engine, you can use Visual Studio to debug
your application in script mode. If you’re using ClearScript’s new V8-based JavaScript
engine, your best option is <a href="https://code.visualstudio.com/">Visual
Studio Code</a>. Best of all is that you can attach both Visual Studio and Visual
Studio Code to the same process for simultaneous debugging of script, managed,
and native code. See <a
href="https://microsoft.github.io/ClearScript/Details/Build.html">here</a> for
the latest information about using ClearScript with V8.</p>

<h1 style='margin-left:.25in;text-indent:-.25in'>27. How can I learn more?</h1>

<p class=MsoNormal style='margin-left:.25in'>Take a look at the ClearScript <a
href="https://microsoft.github.io/ClearScript/Reference/html/R_Project_Reference.htm">API
reference</a> that accompanies this FAQtorial. Also check out the <a
href="https://github.com/Microsoft/ClearScript/tree/master/ClearScriptConsole"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>ClearScriptConsole</span></b></a>
and <a
href="https://github.com/Microsoft/ClearScript/tree/master/ClearScriptBenchmarks"><b><span
style='font-size:10.0pt;line-height:115%;font-family:Consolas'>ClearScriptBenchmarks</span></b></a>
test programs.</p>

</div>

</body>

</html>
